﻿using Microsoft.Xna.Framework;
using ShevaEngine.Core.Modules.Materials;
using ShevaEngine2GUI.DrawObjects;

namespace ShevaEngine2GUI
{
    /// <summary>
    /// Mouse cursor.
    /// </summary>
    public class MouseCursor : Control
    {
        /// <summary>Position.</summary>
        public Vector2 Position;
        /// <summary>Center offset.</summary>
        public Vector2 CenterOffset;
        /// <summary>Image rectangle.</summary>
        private ImageRectangle ImageRectangle;

        /// <summary>Cursor moved.</summary>
        public event OnMouseCursorMove CursorMoved;

        public static readonly MouseCursor Default = new MouseCursor(
            new ImageRectangle(TextureManager.Instance.GetTexture("mouseCursor"), new Rectangle(0,0,32,32)));

        /// <summary>
        /// Constructor.
        /// </summary>
        public MouseCursor(ImageRectangle imageRectangle)
            : base()
        {
            this.ImageRectangle = imageRectangle;
            this.Position = Vector2.Zero;
            this.CenterOffset = Vector2.Zero;
        }

        /// <summary>
        /// Method Draw().
        /// </summary>
        public override void Draw()
        {
            this.ImageRectangle.PositionSize.X = (int)(this.Position.X + this.CenterOffset.X);
            this.ImageRectangle.PositionSize.Y = (int)(this.Position.Y + this.CenterOffset.Y);

            this.ImageRectangle.Draw();
        }

        /// <summary>
        /// Method sets Mouse cursor position.
        /// </summary>
        /// <param name="position"></param>
        public void SetCursorPosition(Vector2 position)
        {
            if (position != this.Position)
            {
                this.Position.X = position.X;
                this.Position.Y = position.Y;

                if (this.CursorMoved != null)
                    this.CursorMoved(this, this.Position);
            }
        }
    }

    /// <summary>
    /// Mouse cursor move callback.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="position"></param>
    public delegate void OnMouseCursorMove(object sender, Vector2 position);
}
